//
//  ShardDetailCell.m
//  Pianke
//
//  Created by BetaTheta on 16/1/22.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "ShardDetailCell.h"

@implementation ShardDetailCell

// 内存管理
- (void)dealloc
{
    [_iconImg release];
    [_unameLabel release];
    [_timeLabel release];
    [_contentLabel release];
    [_backImg release];
    [_webView release];
    [_shardModel release];
    [super dealloc];
}
// 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //
        self.iconImg = [[UIImageView alloc] init];
        _iconImg.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_iconImg];
        [_iconImg release];
        
        //
        self.unameLabel = [[UILabel alloc] init];
        _unameLabel.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_unameLabel];
        [_unameLabel release];
        
        //
        // 时间
        self.timeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_timeLabel];
        [_timeLabel release];
        
        // 内容
        self.contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 0;
        [self.contentView addSubview:_contentLabel];
        [_contentLabel release];
        // 图片
        self.backImg = [[UIImageView alloc] init];
        [self.contentView addSubview:_backImg];
        [_backImg release];
        _backImg.backgroundColor = [UIColor blueColor];
    }
    return self;
}
// 布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    _iconImg.frame = CGRectMake(10, 10, 30, 30);
    // 将头像切成圆形.
    _iconImg.layer.cornerRadius = _iconImg.frame.size.width / 2;
    _iconImg.clipsToBounds = YES;
    
    _unameLabel.frame = CGRectMake(40, 10, 200, 30);
    _timeLabel.frame = CGRectMake(300, 10, 80, 30);
    _timeLabel.font = [UIFont systemFontOfSize:14];
    
    //     通过传值过来的Model进行计算.
    CGFloat h = [[self class] heightWithModel:self.shardModel];
    _backImg.frame = CGRectMake(10, 50, self.contentView.frame.size.width - 20, h);
    // 设置内容自适应高度
    CGFloat h1 = [[self class] heithtWithText:_contentLabel.text];
    
    _contentLabel.frame = CGRectMake(10, 50 + h + 10, self.contentView.frame.size.width - 20, h1);
    
    //     webView 的frame
    //   _webView.frame = CGRectMake(10, 50, self.contentView.frame.size.width - 20,200);
    
}
+(CGFloat)heightWithModel:(ShardModel *)mod
{
    // 高度赋初值
    CGFloat h = 0;
    if (mod.coverimg_wh.length) {
        // 将图片的比例字符串通过*拆分为一个数组.
        NSArray *arr = [mod.coverimg_wh componentsSeparatedByString:@"*"];
        // 通过比例计算h
        h = 355 * ([arr[1] floatValue] / [arr[0] floatValue]);
    }
    return h;
}

+(CGFloat)heithtWithText:(NSString *)text
{
    // 设置属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    // 设置 size
    CGSize size = CGSizeMake(355, 2000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
